#!/bin/sh
###############################################################################
##  Author    : xuezhoyi
##  Name      : edw_parallel_proc_launcher.sh
##  Functions : 
##  Purpose   : 
##  Revisions or Comments
##  VER        DATE        AUTHOR           DESCRIPTION
##---------  ----------  ---------------  ------------------------------------ 
##  1.0      2017-05-22  xuezhouyi        1. CREATED THIS SHELL.
###############################################################################

. ${0%`basename ${0}`}edw_parm.sh
. ${0%`basename ${0}`}edw_func.sh

function USAGE(){
    echo -e "\nHow to use this shell script!"
    echo -e '\n${V_SHELL_HOME}/edw_parallel_proc_launcher.sh ORG'
    echo -e '\n${V_SHELL_HOME}/edw_parallel_proc_launcher.sh ALL'
}

# Check variable
if [[ -z ${1} ]];then
    USAGE
    exit 1
fi

# Define basic parm
V_SCRIPT_NAME=$(basename ${0} | cut -d'.' -f1)
V_OUT_LOGS="${V_SHELL_LOGS}/${V_SCRIPT_NAME}_${V_TIME_STAMP}_$$.log"

# Define the output flow
exec 4>&1               # screen output
exec 3>>${V_OUT_LOGS}   # script output
exec 2>&3               # error output
exec 1>&3               # standard output
echo -e "[Begin: $(date +%F) $(date +%T)]"

# Get parm
V_MODE=${1}

# Switch case
case ${V_MODE} in
    FIN_ORG)
        export V_PARALLEL_NO=1
        # Get org info
        V_QUERY="SELECT ORG_NO FROM ETL.EDW_ORG_INFO WHERE IF_VALID = 'Y';"
        V_ORG_LIST=($(${V_RUN_MYSQL} -q "${V_QUERY}"))
        if [[ $? -ne 0 ]];then
            echo -e "\nERROR:Can not get the org list"
            echo -e "\n${V_QUERY}"
            exit 1
        else
            echo -e "\nSuccess to get the org list."
        fi

        # Parallel run procedure
        echo -e "\n[------------------------Parallel Run Procedure------------------------]"
        for ((i=0; i<${#V_ORG_LIST[@]}; i++));do
            sh ${V_RUN_PROCS} ${V_ORG_LIST[i]} 0 &
            V_PROC_PID[i]=$(echo ${!})
            echo -e "Start to call[${V_PROC_PID[i]}]:${V_RUN_PROCS} ${V_ORG_LIST[i]} 0"
            sleep 2;
        done

        # Check the procedure PID
        echo -e "\nStarting to check procedure..."
        for ((i=0; i<${#V_PROC_PID[@]}; i++));do
            # Get the return code
            wait ${V_PROC_PID[i]}
            if [[ $? -eq 0 ]];then
                echo -e "${V_PROC_PID[i]} has done."
            else
                x=fail
                echo -e "ERROR[${V_PROC_PID[i]}]:Fail to run the procedure."
            fi
        done

        # Check all
        if [[ ${x} == "fail" ]];then
            exit 1
        fi
    ;;
    ALL_ORG)
        export V_PARALLEL_NO=${V_PARALLEL_NUM}
        # Parallel run procedure
        echo -e "\n[------------------------Parallel Run Procedure------------------------]"
        for ((i=0; i<${V_PARALLEL_NUM}; i++));do
            sh ${V_RUN_PROCS} ALL_ORG ${i} &
            V_PROC_PID[i]=$(echo ${!})
            echo -e "Start to call[${V_PROC_PID[i]}]:${V_RUN_PROCS} ${ORG} ${i}"
            sleep 2
        done
        
        # Check the procedure PID
        echo -e "\nStarting to check procedure..."
        for ((i=0; i<${#V_PROC_PID[@]}; i++));do
            # Get the return code
            wait ${V_PROC_PID[i]}
            if [[ $? -eq 0 ]];then
                echo -e "${V_PROC_PID[i]} has done."
            else
                x=fail
                echo -e "ERROR[${V_PROC_PID[i]}]:Fail to run the procedure."
            fi
        done

        # Check all
        if [[ ${x} == "fail" ]];then
            exit 1
        fi
    ;;
esac

echo -e "\n[End: $(date +%F) $(date +%T)]"
echo 0 >&4
exit 0
